mini.parse();

function RegionHandler() {
};

RegionHandler.instance = new RegionHandler();

RegionHandler.isQueryBtnExpended = false;

//获得全局的配送区域数据表格对象
RegionHandler.regionTreeGrid = mini.get("regionTreeGrid");

//加载表格数据
//RegionHandler.regionTreeGrid.load();

/**
 * 根据用户权限展示功能按钮
 */
RegionHandler.loadToolbar = function() {
	var array = new Array("region_list_add", "region_list_edit", "region_list_del", "region_list_query", "region_list_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};

RegionHandler.loadToolbar();

/**
 * 加载树列表
 */
RegionHandler.loadTree = function(){
	RegionHandler.regionTreeGrid.load(__root + "/lazyLoadRegionTreeGrid.do");
}

/**
 * 弹出添加配送区域窗口
 */
RegionHandler.addRegion = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(381, 259, "添加配送区域"); 
	miniWin.setOption("bodyStyle","padding:0px;");
	
	var rowData = RegionHandler.regionTreeGrid.getSelectedNode();	
	var selectedId = null;
	var selectedName = null;
	var selectedBdrType = null;
	
	if(rowData != null){
		selectedId = rowData.bdrId;
		selectedName = rowData.bdrName;
		selectedBdrType = rowData.bdrType;
	}

	miniWin.load("regionForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new",bdrId: selectedId, bdrName: selectedName, bdrType: selectedBdrType};
        iframe.contentWindow.RegionFormHandler.passData(data);
	},function(action){
		if(rowData != null){
			if(rowData.bdrParent == "0"){
				RegionHandler.loadTree();
			}else{
				var parentNode = RegionHandler.regionTreeGrid.getParentNode(rowData);
    			RegionHandler.regionTreeGrid.loadNode(parentNode);
			}
		}else{
			RegionHandler.loadTree();
		}
	});
	
	miniWin.show();
};

/**
 * 弹出编辑配送区域窗口
 */
RegionHandler.editRegion = function() {
	var rowData = RegionHandler.regionTreeGrid.getSelectedNode();
	if(rowData != null){
		miniWin.afreshNewWin();
		miniWin.setParams(381, 259, "编辑配送区域[<font color='blue'>"	+ rowData.bdrName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("regionForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bdrId: rowData.bdrId};
            iframe.contentWindow.RegionFormHandler.passData(data);
		},function(action){
			if(rowData.bdrParent == "0"){
				RegionHandler.loadTree();
			}else{
				var parentNode = RegionHandler.regionTreeGrid.getParentNode(rowData);
    			RegionHandler.regionTreeGrid.loadNode(parentNode);
			}
		});
		
		miniWin.show();
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除配送区域资源
 */
RegionHandler.delRegion = function() {
	var rowData = RegionHandler.regionTreeGrid.getSelectedNode();

	if(rowData){
		mini.confirm("确认删除该区域及其下节点？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					// 判断当前区域或其子区域是否被使用
					if(RegionHandler.getBraByBdr(rowData.bdrId, rowData.bdrType)){
						MiniuiTools.miniWarning("系统提示", "选择的记录或子记录存在收货地址关联，请检查后重试！");
						return;	
					}
					
                    $.ajax({
                        url: __root + "/delBdrById.do",
                        data: {
				        	bdrId: rowData.bdrId,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		if(rowData.bdrParent == "0"){
									RegionHandler.loadTree();
								}else{
									var parentNode = RegionHandler.regionTreeGrid.getParentNode(rowData);
					    			RegionHandler.regionTreeGrid.loadNode(parentNode);
								}
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 检查该区域及其子区域下面是否有相应的收货地址，如果有返回true，反之返回false。
 * @param {} bdrId
 * @param {} bdrType
 * @return {}
 */
RegionHandler.getBraByBdr = function(bdrId, bdrType){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBraByBdr.do",
        dataType: 'json',
        data: {
        	bdrId: bdrId,
        	bdrType: bdrType
        },
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 懒加载treegrid加载驱动
 * @param {} e
 */
RegionHandler.onBeforeTreeLoad = function(e) {
	var tree = e.sender;    //树控件
    var node = e.node;      //当前节点
    var params = e.params;  //参数对象
}

/**
 * 更新配送区域状态
 */
RegionHandler.updateBdrState = function() {
	if (!MiniuiTools.hasPurview("region_list_edit")) {
		return;
	}
	var selectedNode = RegionHandler.regionTreeGrid.getSelectedNode();
	var bdrId = selectedNode.bdrId;
	RegionHandler.regionTreeGrid.loading();
	
    $.ajax({
        url: __root + "/updateBdrStateById.do",
        data: {
        	bdrId: bdrId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
    		if(selectedNode.bdrParent == "0"){
    			RegionHandler.loadTree();
    		}else{
    			var parentNode = RegionHandler.regionTreeGrid.getParentNode(selectedNode);
    			RegionHandler.regionTreeGrid.loadNode(parentNode);
    		}
    		RegionHandler.regionTreeGrid.unmask();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	RegionHandler.regionTreeGrid.unmask();
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });	
};

/**
 * 展开或折叠查询配送区域面板
 */
RegionHandler.queryRegion = function() {
	if (RegionHandler.isQueryBtnExpended) {
		RegionHandler.isQueryBtnExpended = false;
		mini.get("region_list_query").setText("展开查询");
		mini.get("region_list_query").setIconCls("icon-btn-expand");
	} else {
		RegionHandler.isQueryBtnExpended = true;
		mini.get("region_list_query").setText("折叠查询");
		mini.get("region_list_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryRegionDiv").toggle(500);
    setTimeout(function(){mini.get("regionPanel").doLayout();},600);
};

/**
 * 执行查询配送区域的方法
 */
RegionHandler.doQueryRegion = function(){
	var bdrName = mini.get("query_bdrName").getValue();
	
	if (bdrName == "") {
         RegionHandler.regionTreeGrid.clearFilter();
    } else {
		RegionHandler.regionTreeGrid.filter(function (node) {
			var text = node.bdrName;
			
		    if (text.indexOf(bdrName) != -1) {
		        return true;
		    }
		});
    }
};

/**
 * 刷新页面数据及布局
 */
RegionHandler.afresh = function(){
	var selectedNode = RegionHandler.regionTreeGrid.getSelectedNode();
	if(selectedNode){
		RegionHandler.regionTreeGrid.loadNode(selectedNode);
	}else{
		//重新载入
		RegionHandler.loadTree();
	}
}

/**
 * DrawCell 绘制单元格
 */
RegionHandler.onDrawCell = function(e){
     field = e.field,
     value = e.value;
     
     if (field == "bdrType") {
        if (value == 0) {
            e.cellHtml = "国家";                    
        }else if(value == 1){
            e.cellHtml = "省份";
        }else if(value == 2){
            e.cellHtml = "城市";
        }else if(value == 3){
            e.cellHtml = "区县";
        }
     }
     
     if (field == "bdrState") {
        if (value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='RegionHandler.updateBdrState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='RegionHandler.updateBdrState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
     }
}

RegionHandler.loadTree();
